<?php
//ini_set('display_errors', 0);
//error_reporting(0);
set_time_limit(220);


//Line Break
if (PHP_SAPI === 'cli'){
    //if run from command line
    $br = PHP_EOL;
}
else{
    //if run from browser
    $br = "<br/>";
}

use \Crawler\WebCrawler as WebCrawler;

$crawler = WebCrawler::getInstance();

//Crawler for aplopio
//$crawler->beginCrawl("http://ibongda.vn/ty-le-keo-bong-da-ngoai-hang-anh-s1.sr");

// $aplopio = $crawler->getWebPages();
// print_r($aplopio);
// foreach ($aplopio as $url => $properties) {
//     echo $url . $br;
// }
//http://ibongda.vn/?c=statistics&m=reload_home_away_index&type=all_match&season_id=1&season_time_id=29
$url = "http://ibongda.vn/bang-xep-hang-bong-da/ngoai-hang-anh-s1.bxh";
		$arrayxephang = array();
        //for ssl
        $ctx = stream_context_create(
            array(
              'http'=>array(
                'header'=>"Content-type: application/x-www-form-urlencoded",
              )
            )
          );

        $HTML = file_get_contents($url,0,$ctx);
        $doc = new \DOMDocument();
        @$doc->loadHTML($HTML);
        $doc->preserveWhiteSpace = false;
        //$data = $doc->getElementById('table-rank-team');
        //$doc->saveHTML($data);
        //CONTENT OF DATA
        //$data->C14N();
        $tables = $doc->getElementById('table-rank-team');
 
        //$table = $tables->item(0);
        echo '<pre>';
        
        if($tables->childNodes->length > 0){
        	//Tung dong
        	foreach ($tables->childNodes as $keyr => $row) {
        		//print_r($row);
        		//echo '<br>';
        		if($row->childNodes->length > 0){
        			$arrtmp = array();
        			//Tung cot
        			foreach ($row->childNodes as $keytd => $td) {
        				# code...
        				//print_r($td);
        				if($td->nodeName == 'td'){
        					//echo $td->nodeValue;
        					//echo '-';
        					$arrtmp[]=$td->nodeValue;
        				}
        			}
        			$param = array();
        			$param['no']=$arrtmp[0];
        			$param['team']=$arrtmp[1];
        			$param['matches']=$arrtmp[2];
        			$param['w']=$arrtmp[3];
        			$param['d']=$arrtmp[4];
        			$param['l']=$arrtmp[5];
        			$param['goals_win']=$arrtmp[6];
        			$param['goals_lost']=$arrtmp[7];
        			$param['point']=$arrtmp[8];

        			$arrayxephang[]=$param;
        		}

        	}
        }
        print_r($arrayxephang);

        $file = fopen("json_bangxephang.txt","w");
        fwrite($file,json_encode(array('data'=>$arrayxephang)));
        fclose($file);

//autoload of classes
function __autoload($className) {
    $filename = $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}
